home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapter6 / getmchmd.c < prev    next >
C/C++ Source or Header  |  1996-01-27  |  7KB  |  220 lines

  1.  
  2. #include <windows.h>  
  3. #include "GetMChMD.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109. static HBITMAP hMemBitmap;
  110.  
  111.    switch( uMsg )
  112.    {
  113.       case WM_CREATE :
  114.             {
  115.                HDC     hSrcDC, hDstDC;
  116.                BITMAP  bm;
  117.                HBITMAP hChkBitmap;
  118.  
  119.                HMENU hMenu      = GetMenu( hWnd );
  120.                LONG  lCheckSize = GetMenuCheckMarkDimensions();
  121.                SHORT nBx        = LOWORD( lCheckSize );
  122.                SHORT nBy        = HIWORD( lCheckSize );
  123.                HDC   hDC        = GetDC( hWnd );
  124.                
  125.                // Create a memory DC to select the bitmap into.
  126.                //..............................................
  127.                hSrcDC = CreateCompatibleDC( hDC );
  128.                hChkBitmap = LoadBitmap( hInst, "Checkmark" );
  129.  
  130.                SelectObject( hSrcDC, hChkBitmap );
  131.  
  132.                GetObject( hChkBitmap, sizeof( BITMAP), &bm );
  133.  
  134.                // Create a memory DC to stretch the bitmap into.
  135.                //...............................................
  136.                hDstDC     = CreateCompatibleDC( hDC );
  137.                hMemBitmap = CreateCompatibleBitmap( hDstDC, nBx, nBy );
  138.                SelectObject( hDstDC, hMemBitmap );
  139.  
  140.                StretchBlt( hDstDC, 0, 0, nBx, nBy, hSrcDC, 0, 0,
  141.                            bm.bmWidth, bm.bmHeight, SRCCOPY );
  142.  
  143.                // Set the bitmap to use for checking to the newly sized
  144.                // bitmap.
  145.                //......................................................
  146.                SetMenuItemBitmaps( hMenu, IDM_CHKITEM, MF_BYCOMMAND, NULL,
  147.                                    hMemBitmap );
  148.  
  149.                DeleteDC( hSrcDC );
  150.                DeleteDC( hDstDC );
  151.                ReleaseDC( hWnd, hDC );
  152.                DeleteObject( hChkBitmap );
  153.             }
  154.             break;
  155.  
  156.       case WM_COMMAND :
  157.               switch( LOWORD( wParam ) )
  158.               {
  159.                  case IDM_CHKITEM :
  160.                       { 
  161.                          HMENU hMenu  = GetMenu( hWnd );
  162.                          UINT  uState = GetMenuState( hMenu,
  163.                                                       IDM_CHKITEM,
  164.                                                       MF_BYCOMMAND );
  165.  
  166.                          if ( uState & MFS_CHECKED )
  167.                             CheckMenuItem( hMenu, IDM_CHKITEM, 
  168.                                            MF_BYCOMMAND | MFS_UNCHECKED );
  169.                          else
  170.                             CheckMenuItem( hMenu, IDM_CHKITEM,
  171.                                            MF_BYCOMMAND | MFS_CHECKED );
  172.                       }
  173.                       break;
  174.  
  175.                  case IDM_ABOUT :
  176.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  177.                         break;
  178.  
  179.                  case IDM_EXIT :
  180.                         DestroyWindow( hWnd );
  181.                         break;
  182.               }
  183.               break;
  184.       
  185.       case WM_DESTROY :
  186.               DeleteObject( hMemBitmap );
  187.               PostQuitMessage(0);
  188.               break;
  189.  
  190.       default :
  191.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  192.    }
  193.  
  194.    return( 0L );
  195. }
  196.  
  197.  
  198. LRESULT CALLBACK About( HWND hDlg,           
  199.                         UINT message,        
  200.                         WPARAM wParam,       
  201.                         LPARAM lParam)
  202. {
  203.    switch (message) 
  204.    {
  205.        case WM_INITDIALOG: 
  206.                return (TRUE);
  207.  
  208.        case WM_COMMAND:                              
  209.                if (   LOWORD(wParam) == IDOK         
  210.                    || LOWORD(wParam) == IDCANCEL)    
  211.                {
  212.                        EndDialog(hDlg, TRUE);        
  213.                        return (TRUE);
  214.                }
  215.                break;
  216.    }
  217.  
  218.    return (FALSE); 
  219. }
  220.